package com.example.xd.phone_album

import android.app.Activity
import android.content.ContentValues
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import kotlinx.android.synthetic.main.select_pic_fragment.*


/**
 * @auther XuDan on 2020/8/13.
 */
const val DETIAL_CODE = 1
const val TAKE_PIC_CODE = 2


class SelectPicFragment : ScanFragment(), BaseQuickAdapter.OnItemClickListener,
    BaseQuickAdapter.OnItemChildClickListener {
    var count = 0
    var selectList: MutableList<SingleImageModel> = mutableListOf()


    fun takePicSuccess() {}
    fun addNewFile(
        bitmap: Bitmap,
        displayName: String,
        mimeType: String,
        compressFormat: Bitmap.CompressFormat
    ) {
        val values = ContentValues()
        val contentResolver = activity!!.getContentResolver()
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
        values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM)
        } else {
            values.put(
                MediaStore.MediaColumns.DATA,
                "${Environment.getExternalStorageDirectory().path}/${Environment.DIRECTORY_DCIM}/$displayName"
            )
        }
        val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        if (uri != null) {
            val outputStream = contentResolver.openOutputStream(uri)
            if (outputStream != null) {
                bitmap.compress(compressFormat, 100, outputStream)
                outputStream.close()
            }
        }
    }

    fun select(position: Int) {
        val file = scanFileList.get(position)
        val select = file.isSelect
        file.isSelect = !select
        if (file.isSelect) {
            selectList.add(file)
        } else {
            selectList.remove(file)
        }
        count = 0
        for (value in selectList) {
            for (scan_value in scanFileList) {
                if (value.path == scan_value.path) {
                    scan_value.ordinal = ++count
                }
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.select_pic_fragment
    }

    override fun initView(view: View) {
        fg_pic_recycler.layoutManager = GridLayoutManager(context, 4)
        val pictureAdapter =
            PictureAdapter(context!!, R.layout.recycler_item_pic_layout, scanFileList)
        pictureAdapter.setOnItemClickListener(this)
        pictureAdapter.setOnItemChildClickListener(this)
        fg_pic_recycler.adapter = pictureAdapter
    }

    override fun getScanType(): Int {
        return MEDIA_TYPE_PIC
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when (position) {
            0 -> {
                startActivityForResult(
                    Intent(context, TakePicActivity::class.java),
                    TAKE_PIC_CODE
                )
            }
            else -> {
                var intent = Intent(activity, DetialActivity::class.java)
                intent.putExtra("file", scanFileList.get(position))
                intent.putExtra("select_size", selectList.size)
                intent.putExtra("scan_size", scanFileList.size)
                startActivityForResult(intent, DETIAL_CODE)
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            TAKE_PIC_CODE -> {
                if (resultCode == Activity.RESULT_OK) {
                    val singleImageModel = SingleImageModel()
                    singleImageModel.path = ""
                    singleImageModel.isSelect = true
                    scanFileList.add(0, singleImageModel)
                }
            }
            DETIAL_CODE -> {

            }

        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when (view.id) {
            R.id.recycler_item_tv_select -> {
                select(position)
                adapter.notifyDataSetChanged()
            }
        }
    }
}